/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yutel.wuzi.entities;

import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author yuanyajun
 */
public class ChessPad {

    private Graphics g;
    private Mouse mouse;
    private int dx;
    private int dy;
    private int size;

    public void drawChessboard() {
        g.setColor(255, 255, 0);
        g.fillRect(0, 0, dx * 16, dy * 16);
        g.setColor(255, 0, 0);
        for (int i = 0; i < 15; i++) {
            int x = dx * (i + 1);
            g.drawLine(x, dy, x, dy * 15);
        }
        for (int i = 0; i < 15; i++) {
            int y = dy * (i + 1);
            g.drawLine(dx, y, dx * 15, y);
        }
    }
    public void drawMessage(String message) {
        g.setColor(0,0,255);
        g.drawString(message, dx, 0, Graphics.TOP|Graphics.LEFT);
    }

    public void drawChesses(Vector step) {
        Chess ch = null;
        for (Enumeration e = step.elements(); e.hasMoreElements();) {
            ch = (Chess) e.nextElement();
            ch.draw(g, dx, dy, size);
        }
        if (ch != null) {
            ch.drawCurrent(g, dx, dy, size);
        }
    }

    public void drawMouse() {
        this.getMouse().draw(g, dx, dy, size);
    }

    public Chess addChess() {
        Chess chess = new Chess(this.getMouse().getX(), this.getMouse().getY());
        return chess;
    }

    public void setSize(int width, int height) {
        this.dx = width / 16;
        this.dy = height / 16;
        if (dx >= dy) {
            this.size = (this.dy - 1) / 2;
        } else {
            this.size = (this.dx - 1) / 2;
        }
    }

    public ChessPad() {
        this.dx = 11;
        this.dy = 11;
        this.size = 5;
    }

    public void setGraphics(Graphics g) {
        this.g = g;
    }

    public Mouse getMouse() {
        if (mouse == null) {
            mouse = new Mouse();
            return mouse;
        } else {
            return mouse;
        }
    }
}
